package com.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.pojo.Caiwu;
import com.pojo.Employee;
import com.pojo.Kaoqin;
import com.pojo.Project;
import com.service.EmployeeService;
import com.service.KaoqinService;

@Controller
@RequestMapping("/kaoqin")
public class KaoqinController {

	@Autowired
	private KaoqinService kaoqinService;
	@Autowired
	private EmployeeService employeeService;

	@RequestMapping(value = "/caiwu")
	public String listUsers(Model model) {

		List<Caiwu> caiwus = new ArrayList<>();
		List<Employee> employees = employeeService.GetAllEmployee();

		for (Employee e : employees) {

			int quanqin = 0;
			int count_chizao = 0;
			int count_qingkuang = 0;
			int qingkuang = 0;
			int chizao = 0;
			Caiwu caiwu = new Caiwu();
			caiwu.setEid(e.getEmployee_Id());
			caiwu.setName(e.getEmployee_Name());
			caiwu.setJiben(e.getEmployee_Salary());

			List<Kaoqin> kaoqin = kaoqinService.allByEmployee(e.getEmployee_Id());
			String type;
			for (Kaoqin k : kaoqin) {
				type = k.getKaoqin_Type();
				if (type.equals("迟到") || type.equals("早退")) {
					count_chizao++;
				} else
					count_qingkuang++;
			}
			if (count_chizao == 0 && count_qingkuang == 0)
				quanqin = 300;
			chizao = count_chizao * 20;
			qingkuang = count_qingkuang * 100;
			caiwu.setChizao(chizao);
			caiwu.setChizao_count(count_chizao);
			caiwu.setQingkuang(qingkuang);
			caiwu.setQingkuang_count(count_qingkuang);
			caiwu.setQuanqin(quanqin);
			caiwu.setShiji(caiwu.getJiben() + quanqin - chizao - qingkuang);
			caiwus.add(caiwu);
		}
		model.addAttribute("caiwus", caiwus);
		return "/jsp/finance";
	}

	// @RequestMapping(value = "/employee")
	// public String employee(Model model,@RequestParam Integer eid) {
	//
	// //根据eid找记录
	//
	// model.addAttribute("kaoqins", caiwus);
	// return "/jsp/kaoqing";
	// }

	@RequestMapping(value = "/kaoqin")
	public String listkaoqin(Model model) {

		List<Caiwu> caiwus = new ArrayList<>();
		List<Employee> employees = employeeService.GetAllEmployee();

		for (Employee e : employees) {

			Caiwu caiwu = new Caiwu();
			int quanqin = 0;
			int count_chizao = 0;
			int count_qingkuang = 0;
			int qingkuang = 0;
			int chizao = 0;
			int zaotui = 0;
			int kuangban = 0;
			caiwu.setEid(e.getEmployee_Id());
			caiwu.setName(e.getEmployee_Name());
			caiwu.setJiben(e.getEmployee_Salary());

			List<Kaoqin> kaoqin = kaoqinService.allByEmployee(e.getEmployee_Id());
			String type = new String();
			for (Kaoqin k : kaoqin) {

				type = k.getKaoqin_Type();
				if (type.equals("迟到")) {
					count_chizao++;
				} else if (type.equals("请假"))
					count_qingkuang++;
				else if (type.equals("早退"))
					zaotui++;
				else
					kuangban++;
			}
			if (count_chizao == 0 && count_qingkuang == 0)
				quanqin = 300;
			chizao = count_chizao * 20;
			qingkuang = count_qingkuang * 100;
			caiwu.setChizao(chizao);
			caiwu.setChizao_count(count_chizao);
			caiwu.setQingkuang(qingkuang);
			caiwu.setQingkuang_count(count_qingkuang);
			caiwu.setQuanqin(quanqin);
			caiwu.setShiji(caiwu.getJiben() + quanqin - chizao - qingkuang);
			caiwu.setZaotui(zaotui);
			caiwu.setKuangban(kuangban);
			caiwus.add(caiwu);
			for (Caiwu c : caiwus) {
				System.out.println(c);
			}
		}
		model.addAttribute("kaoqins", caiwus);
		return "/jsp/kaoqing";
	}

	@RequestMapping(value = "/toKaoqin_add")
	public String toKaoqin_add(Model model) {
		model.addAttribute("kaoqin", new Kaoqin());
		return "/jsp/kaoqin_add";
	}

	@RequestMapping(value = "/kaoqin_add")
	public String Kaoqin_add(@ModelAttribute Kaoqin kaoqin, Model model) {
		// 先查看是否存在该用户
		Employee e = employeeService.GetEmployee(kaoqin.getKaoqin_Emp_Id());
		String url = "";
		// 不存在
		if (e == null) {
			String error = "添加失败";
			model.addAttribute("kaoqin", kaoqin);
			model.addAttribute("error", error);
			url = "jsp/kaoqin_add";
		} else if (kaoqinService.addKaoqin(kaoqin))
			url = "redirect:../kaoqin/kaoqin";
		return url;
	}

	@RequestMapping(value = "/employee")
	public String tokaoqin_employee(@RequestParam String name, @RequestParam Integer eid, Model model) {
		// 根据eid查找考勤记录
		List<Kaoqin> kaoqins = kaoqinService.allByEmployee(eid);
		model.addAttribute("kaoqins", kaoqins);
		model.addAttribute("name", name);
		model.addAttribute("eid", eid);
		return "jsp/kaoqin_employee";
	}

	@RequestMapping(value = "employee/delete")
	public String delete(@RequestParam Integer kid, Model model) {
		String url = "";
		Integer employee_id;
		employee_id = kaoqinService.GetKaoqin(kid).getKaoqin_Emp_Id();
		Employee emp = kaoqinService.GetEmployee(employee_id);
		model.addAttribute("name", emp.getEmployee_Name());
		model.addAttribute("eid", emp.getEmployee_Id());
		System.out.println("删除" + kid);
		if (kaoqinService.deletKaoqinForId(kid))
			url = "redirect:../employee";
		else {
			String error = "删除失败";
			model.addAttribute("error", error);
			url = "redirect:../employee";
		}
		return url;
	}

	@RequestMapping(value = "employee/toupdate")
	public String toupdate(@RequestParam Integer kid, Model model) {
		model.addAttribute("kaoqin", kaoqinService.GetKaoqin(kid));
		return "jsp/kaoqin_employee_update";
	}

	@RequestMapping(value = "employee/update")
	public String update(@ModelAttribute Kaoqin kaoqin, Model model) {
		String url = "";
		String error = "";
		//找人
		Employee emp=employeeService.GetEmployee(kaoqin.getKaoqin_Emp_Id());
		System.out.println("更新" + kaoqin);
		if (kaoqinService.updateKaoqin(kaoqin))
			url = "redirect:../employee";
		else {
			error = "更新失败";
			url = "redirect:../employee/toupdate";
		}
		model.addAttribute("error", error);
		model.addAttribute("name", emp.getEmployee_Name());
		model.addAttribute("eid", emp.getEmployee_Id());
		return url;
	}
}